import java.awt.*;
import java.awt.event.*;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

public class MainForm {
	
	private static JFrame frm, pbfrm;
	private static JProgressBar pb;
	private static Button btn_decode, btn_encode;
	static WhoEncode0001 whoenc;
	static WhoDecode0001 whodec;
	
	private void init(){
		whoenc = new WhoEncode0001();
		whodec = new WhoDecode0001();
		
		Font f1 = new Font("San-Serif", Font.PLAIN, 14);
		Font f2 = new Font("Monospaced", Font.BOLD, 38);

		frm = new JFrame("WhoDecode0001");
		
		TextField te = new TextField("svn checkout http://dearestwho.googlecode.com/svn/WhoDecode0001-j D:/WhoDecode0001-j");
		te.setFont(f1);
		te.setEditable(false);
		frm.add(te,BorderLayout.SOUTH);
//		frm.add(te,BorderLayout.CENTER);
		
		btn_decode = new Button("decode");
		btn_encode = new Button("encode");
		btn_decode.setFont(f2);
		btn_encode.setFont(f2);
		btn_decode.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				new Thread(){
					public void run(){
						whodec.decode();
						MainForm.closeProgressBar();
					}
				}.start();
			  }
			});
		btn_encode.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				new Thread(){
					public void run(){
						whoenc.encode();
						MainForm.closeProgressBar();
					}
				}.start();
			}
		});
		
		frm.add(btn_decode, BorderLayout.NORTH);
		frm.add(btn_encode, BorderLayout.CENTER);
//		frm.add(btn_encode, BorderLayout.SOUTH);
		
//		frm.setSize(256, 160);
		frm.pack();
		frm.setSize(256, frm.getHeight());
		int scrWidth = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
		int scrHeight = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
		frm.setLocation( (scrWidth - frm.getWidth()) / 2, (scrHeight - frm.getHeight()) / 2);
        frm.setResizable(false);
		frm.setVisible(true);
		frm.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		
		pbfrm = new JFrame("Praying");
		JPanel jpanel = new JPanel();
		jpanel.setLayout(new FlowLayout());
		pb = new JProgressBar(0, 1);
		pb.setValue(0);
        pb.setStringPainted(true);
        jpanel.add(pb);
        pbfrm.setContentPane(jpanel);
        pbfrm.pack();
        pbfrm.setResizable(false);
        pbfrm.setLocation((scrWidth-pbfrm.getWidth())/2, (scrHeight+frm.getHeight())/2);
//        pbfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pbfrm.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(WindowEvent winEvt) {
            	MainForm.closeProgressBar();
            }
        });

	}
	
	public static void main(String[] args) {
		MainForm mf = new MainForm();
		mf.init();
		
	}
	
	public static File[] openFiles(String path, String extNameFilter) {
		JFileChooser file = new JFileChooser(path);
		file.setMultiSelectionEnabled(true);
        FileFilter filter = new FileNameExtensionFilter(extNameFilter+" Files(*."+extNameFilter+")", extNameFilter);
        file.addChoosableFileFilter(filter);
        file.setFileFilter(filter);
		file.showOpenDialog(null);
		File[] files = file.getSelectedFiles();
		MainForm.showProgressBar(files.length * 3);
		return files;
	}
	
	public static void showMsgBox(String msg){
		JOptionPane.showMessageDialog(null,msg);
	}
	
	public static void showProgressBar(int max){
		pb.setMaximum(max);
		pb.setString("dearestwho");
		pb.setValue(0);
        pbfrm.setVisible(true);
        btn_decode.setEnabled(false);
        btn_encode.setEnabled(false);
	}
	public static void setProgressBar(String s, int v){
		pb.setString(s);
		pb.setValue(v);
	}
	public static void setProgressBarValue(int v){
		pb.setValue(v);
	}
	
	public static void closeProgressBar(){
		pbfrm.setVisible(false);
		btn_decode.setEnabled(true);
        btn_encode.setEnabled(true);
        
        whodec.stopMe();
        whoenc.stopMe();
	}

}